// 创建路由
const router = require('express').Router()
// 引入数据库
const db = require('../db')

// 添加到购物车
router.post('/addCart', (req, res) => {
   let cartList = req.body
   let sql2 = `SELECT * FROM cart WHERE user_id=${cartList.user_id} AND goods_id = ${cartList.goods_id}`;
   db.query(sql2, (error, data1) => {
      if (error) {
         res.json({
            "ok": 0,
            "error": error
         })
      } else {
         if (data1.length == 0) {
            let sql = 'INSERT INTO cart SET ?'
            db.query(sql, cartList, (error, data) => {
               if (error) {
                  res.json({
                     "ok": 400,
                     "error": error
                  })
               } else {

                  res.json({
                     "ok": 1,
                     'message': '成功'
                  })
               }
            })
         } else {
            let newCount = data1[0].count + 1
            let id = data1[0].id
            let sql3 = `UPDATE cart SET count= ${newCount} WHERE id  = ${id}`
            db.query(sql3, (error, data) => {
               if (error) {
                  res.json({
                     "error": error
                  })
               } else {
                  res.json({
                     "ok": 1,
                     "data": "商品数量+1"
                  })
               }
            })
         }

      }
   })

})

// 查询购物车商品
router.get('/cart/:id(\\d+)', (req, res) => {
   let id = req.params.id
   // sql查询语句
   let sql = 'SELECT * FROM cart WHERE user_id = ?'
   db.query(sql, id, (error, data) => {
      if (error) {
         res.json({
            'ok': 0,
            'error': error
         })
      } else {
         // 没商品时
         if (data.length === 0) {
            return res.json({
               'ok': 1,
               'data': data,
               'message': '你的购物车还没有商品'
            })
         }
         let goodsId = []
         data.forEach(ele => {
            goodsId.push(ele.goods_id)
         });
         goodsId = goodsId.join(',')
         // 根据goods_id 查询出商品名称、价格和图片
         let sql = `SELECT product_name, price,small_image,id FROM home_tabbar_goods WHERE id in (${goodsId})`
         db.query(sql, (error, data2) => {
            // 合并对象
            data.forEach(item => {
               data2.forEach(item2 => {
                  if (item.goods_id === item2.id) {
                     item.product_name = item2.product_name
                     item.price = item2.price
                     item.small_image = item2.small_image

                  }
               })
            })
            // console.log(data)
            if (error) {
               res.json({
                  'ok': 0,
                  'error': error
               })
            } else {

               res.json({
                  'ok': 1,
                  'data': data
               })
            }
         })
      }
   })
})

// 删除购物车商品
router.delete('/delCart', (req, res) => {
   let id = req.query.cid
   // console.log(id);

   if (id.length > 1) {
      id = id.split(',')

      let wenhao = []
      id.forEach(element => {
         wenhao.push('?')
      });
      wenhao = wenhao.join(',')

      // sql删除语句
      let sql = `delete from cart where id in (${wenhao})`
      db.query(sql, id, (error, data) => {
         if (error) {
            res.json({
               'ok': 0,
               'error': error
            })
         } else {
            res.json({
               'ok': 1
            })
         }
      })

   } else {
      // // 获取id
      // let id = req.body.id
      // sql删除语句
      let sql = 'delete from cart where id = ?'

      db.query(sql, id, (error, data) => {
         if (error) {
            res.json({
               'ok': 0,
               'error': error
            })
         } else {
            res.json({
               'ok': 1
            })
         }
      })

   }
})

// 修改商品选中状态
router.put(`/ischkCart/:id(\\d+)`, (req, res) => {
   // 获取id
   let id = req.params.id
   // 获取参数
   let ischk = req.body.ischk
   // 定义要修改的数据
   let dataList = {}
   // 修改数据放进 datalist
   dataList.ischk = ischk
   // sql语句
   let sql = `UPDATE cart SET ? WHERE id = ?`
   // 执行sql语句
   db.query(sql, [dataList, id], (error, data) => {
      if (error) {
         res.json({
            'ok': 0,
            'error': error
         })
      } else {
         res.json({
            'ok': 1,
            'data': dataList
         })
      }
   })
})

// 修改商品数量
router.put(`/countCart/:id(\\d+)`, (req, res) => {
   // 获取id
   let id = req.params.id
   // 获取参数
   let count = req.body.count
   // 定义要修改的数据
   let dataList = {}
   // 修改数据放进 datalist
   dataList.count = count
   // sql修改语句
   let sql = `UPDATE cart SET ? WHERE id = ?`
   // 执行sql语句
   db.query(sql, [dataList, id], (error, data) => {
      if (error) {
         res.json({
            'ok': 0,
            'error': error
         })
      } else {
         res.json({
            'ok': 1,
            'data': dataList
         })
      }
   })
})

// 暴露路由
module.exports = router